package com.byzk.p2p.admin.base.handler;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import com.byzk.p2p.admin.base.exception.LoginExeption;

/**
 * @author suker.zhou
 * @version 1.0
 */
public class ExceptionHandler  implements HandlerExceptionResolver{
	
	private static final Logger logger = LoggerFactory.getLogger(ExceptionHandler.class);
	
	public ModelAndView resolveException(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex) {
		try {
			response.setCharacterEncoding("utf-8");
			response.setContentType("text/html; charset=utf-8");
			
			// 根据不同错误转向不同页面  
	        if(ex instanceof LoginExeption) {  
				response.sendRedirect("/login?error=3");
	        }else{
	    		logger.error("{}", ex);

				response.getWriter().write("后台报错，请联系管理员" + ex.getMessage());
				response.setStatus(500);
	        } 
		} catch (IOException e) {
			e.printStackTrace();
		}
		return new ModelAndView();
	}

}
